#hivemind
schema and examples

The heart of hivemind beats with the rhythm of encounters, the hivemind is shared among entities.
```
hivemind.encounter (self, clicker, {relation_name = 1})  
```
the mob first creates its own identifier
All encounters between an entity (generally a player) and a mob are recorded as follows
schema:

self.hivemind = {
                 "id" = math.rand (or {territory, name}, etc...)
k:                 [id] = {
v/x:                       [entity] = {
y:                                  [relation_name] = value,
                                    [another_relation] = value,
                                    fe = value (generated from os.time),
                                    le = value (generated from os.time)
                                    }
                        },
k:                 [another_id] = {
v/x:                       [entity] = {
y:                                  [relation_name] = value,
                                    [another_relation] = value,
                                    fe = value (generated from os.time),
                                    le = value (generated from os.time)
                                    }
                        },
                }

* mobs can then transfer relations first by comparing time stamps of "le" (last encounter) from each [id],[entity].le pair 

* if the remote mob has a new or newer dataset for [id][entity] then the local mob dataset for that [id][entity] is added or replaced.

* manipulation of the remotes own dataset should be conducted by that mob with the same function otherwise this can become redundant

* with relations recorded, mobs can simply add the relation values to get data on how to react during an entity encounter 

* we could use relations to keep track of which ids belong to a certain group (such as territory) to determine what is relavent

example using mobs_redo:

init.lua:
```
if minetest.get_modpath("hivemind") then
  print("hivemind mod detected")
  mymod.hivemind = 1
end
```
in your mob definition:
```
do_custom = function(self)
      if mymod.hivemind then
        hivemind.pull(self)
      end    
end,

do_punch = function(self,hitter)
       if mymod.hivemind then
        hivemind.encounter (self, hitter, {naughty = 2})
        print(dump(hivemind.encounter(self)))
       end 
end,

on_rightclick = function(self,clicker)
    if mymod.hivemind then
        hivemind.encounter (self, clicker, {nice = 1})
        print(dump(hivemind.encounter(self)))
    end
end,
```
when making a query against an entity, we can make a simple function like this to get a score (assuming "naughty" is a relation in this example)
```
local function get_aggro(self,target) -- how aggressive is this entity?
  local naughty = 0 --assume the best :D
  for k,v in pairs(self.hivemind) do
    if k = id then end --we can ignore this, it only identifies us 
    else -- we want to know how the target has treated others that we have talked to!
        if v = target and then
            naughty = naughty + v.naughty
        end
    end
   return naughty  --the total naughty to all entity friends
  end 
end
```
we could then compare results of "naughty" and "nice" among groups of mobs as well anything else you could want such as how long a mob has been "alive", "hungry", "exposed" to radiation, etc.

##TL;DR:
* Define or update a table with key(s) and value(s) when you call: 
  hivemind.encounter(self, entity, {something_to_track = 1}) 

* Omit the optional params for queries

* Call hivemind.pull(self) to look for other entities encounters for further processing


